package kr.co.onc.common;

import java.beans.PropertyEditorSupport;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.servlet.http.HttpServletResponse;

import kr.co.onc.util.JsonUtil;

import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;


public class BaseController     {
	public static String REDIRECT="REDIRECT";
	public static String ORDER_MENUCODE="?menuCode=0100";
	public static String CUSTOMER_MENUCODE="?menuCode=0400";
	public static String PRODUCT_MENUCODE="?menuCode=0300";


	@InitBinder
	public void initBinder(WebDataBinder binder)
	{
		class CustomDateEditor extends PropertyEditorSupport
		{
			@Override
			public void setAsText(String text) throws IllegalArgumentException
			{
				SimpleDateFormat dateFormat;
				
				
				if(text.trim().matches("^([0-9]{4}-[0-9]{1,2}-[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2})$"))
				{
					dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				}
				if(text.trim().matches("^([0-9]{4}-[0-9]{1,2}-[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2})$"))
				{
					dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				}
				else if(text.trim().matches("^([0-9]{4}-[0-9]{1,2}-[0-9]{1,2})$"))
				{
					dateFormat = new SimpleDateFormat("yyyy-MM-dd");
				}
				else
				{
					dateFormat = new SimpleDateFormat();
				}
					
				try
				{
					setValue(dateFormat.parse(text));
				}
				catch(ParseException ex)
				{
					setValue((Date)null);
				}
				catch(Exception e)
				{
					setValue((Date)null);
				}
			}
		}
		binder.registerCustomEditor(Date.class, new CustomDateEditor());
	
		class CustomIntEditor extends PropertyEditorSupport
		{
			@Override
			public void setAsText(String text) throws IllegalArgumentException
			{
				try
				{
					setValue(Integer.valueOf(text));
				}
				catch(Exception e)
				{
					setValue((int)0);
				}
			}
		}
		binder.registerCustomEditor(int.class, new CustomIntEditor());
			
			
		class CustomLongEditor extends PropertyEditorSupport
		{
			@Override
			public void setAsText(String text) throws IllegalArgumentException
			{
				try
				{
					setValue(Long.valueOf(text));
				}
				catch(Exception e)
				{
					setValue((long)0);
				}
			}
		}
		binder.registerCustomEditor(long.class, new CustomLongEditor());
	}

	
	protected void ResponseJSON(HttpServletResponse response, String str) throws IOException
	{
		String contentType = response.getContentType();
		
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json");
		PrintWriter pw = response.getWriter();
		
		// JSON 於滊牓
		if(contentType == null || contentType.isEmpty() || contentType.trim().equals(""))
		{
			pw.print(str);
		}
		// APP 頃垬 順胳稖順�
		else
		{
			response.setContentType("text/html");
			
			pw.println("<script type=\"text/javascript\">");
			pw.println("//<![CDATA[");
			pw.println("function callAPP()");
			pw.println("{");
			pw.println("	var userAgent = navigator.userAgent.toLowerCase();");
			pw.println("	if(userAgent.indexOf('iphone') >= 0 || userAgent.indexOf('ipad') >= 0 || userAgent.indexOf('ipod') >= 0)");
			pw.println("		document.location = 'toApp:"+ contentType +":"+ str.replaceAll("'", "\\\\'").replaceAll("\n", "\\\\n") +"';");
			pw.println("	else if(userAgent.indexOf('android') >= 0)");
			pw.println("		window.webBridge."+ contentType +"('"+ str.replaceAll("'", "\\\\'").replaceAll("\n", "\\\\n") +"');");
			pw.println("}");
			pw.println("window.onload = function(){ callAPP(); }");
			pw.println("//]]>");
			pw.println("</script>");
		}
		
		pw.close();
		pw = null;
	}
	
	protected void SendError(HttpServletResponse response, long code, String message) throws IOException
	{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("CODE", code);
		map.put("MESSAGE", message);
		
		String ret = JsonUtil.Map2String(map);
		
		map.clear();
		map = null;
		
		ResponseJSON(response, ret);
	}
	
	protected void SendData(HttpServletResponse response, String name, Object data) throws IOException
	{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(name, data);
		
		String ret = JsonUtil.Map2String(map);
		
		map.clear();
		map = null;
		
		ResponseJSON(response, ret);
	}
	
	protected void SendData(HttpServletResponse response, String name1, Object data1, String name2, Object data2) throws IOException
	{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(name1, data1);
		map.put(name2, data2);
		
		String ret = JsonUtil.Map2String(map);
		
		map.clear();
		map = null;
		
		ResponseJSON(response, ret);
	}
	
	protected void SendData(HttpServletResponse response, String name1, Object data1, String name2, Object data2, String name3, Object data3) throws IOException
	{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(name1, data1);
		map.put(name2, data2);
		map.put(name3, data3);
		
		String ret = JsonUtil.Map2String(map);
		
		map.clear();
		map = null;
		
		ResponseJSON(response, ret);
	}
	
	protected void SendData(HttpServletResponse response, String name1, Object data1, String name2, Object data2, String name3, Object data3, String name4, Object data4) throws IOException
	{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(name1, data1);
		map.put(name2, data2);
		map.put(name3, data3);
		map.put(name4, data4);
		
		String ret = JsonUtil.Map2String(map);
		
		map.clear();
		map = null;
		
		ResponseJSON(response, ret);
	}
	
	protected void SendData(HttpServletResponse response, String name1, Object data1, String name2, Object data2, String name3, Object data3, String name4, Object data4, String name5, Object data5) throws IOException
	{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(name1, data1);
		map.put(name2, data2);
		map.put(name3, data3);
		map.put(name4, data4);
		map.put(name5, data5);
		
		String ret = JsonUtil.Map2String(map);
		
		map.clear();
		map = null;
		
		ResponseJSON(response, ret);
	}
	
	protected Map<String, Object> SetPageInfo(long PAGE, long PAGE_SIZE, long TOTAL_COUNT)
	{
		Map<String, Object> data = new HashMap<String, Object>();
		
		data.put("PAGE", PAGE);
		data.put("PAGE_SIZE", PAGE_SIZE);
		data.put("TOTAL_COUNT", TOTAL_COUNT);
		
		return data;
	}
	
	
	
	
	/**
	 * Properties 臁绊殞
	 * 
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings ( "rawtypes" )
	public static Map<String, String> getProperties ( String propFile ) throws Exception {
		Properties prop = new Properties ();
		Map<String, String> confMap = new HashMap<String, String> ();
		
		
		Enumeration propNames = prop.propertyNames ();
		while ( propNames.hasMoreElements () ) {
			String name = (String) propNames.nextElement ();
			String value = prop.getProperty (name);
			
			confMap.put (name, value);
		}
		
		return confMap;
	}
}
